package UDPSIDE;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @创建人 MBY381
 * @创建时间 2022/6/7 16:06
 * @描述 暂无
 */
public class UDPUI {
    private static JFrame jFrame;
    private static final Font MY_FONT = new Font("宋体", Font.BOLD, 30);
    private static UDPClientThread udpSenderThread;

    public static void main(String[] args) throws SocketException, UnknownHostException {
        udpSenderThread = new UDPClientThread("192.168.13.249", 8888);
        setSelectJFrameJPanel();
    }

    private static void setSelectJFrameJPanel() {

        //设置JFrame

        jFrame = new JFrame("登陆系统");
        jFrame.setFont(MY_FONT);
        jFrame.setSize(350, 200);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置JPanel
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 350, 200);

        jFrame.add(panel);
        placeComponents(panel);
        jFrame.setVisible(true);

    }

    private static void placeComponents(final JPanel panel) {       //设置布局
        panel.setLayout(null);

        //ip地址标签
        JLabel userLabel = new JLabel("账号:");
        userLabel.setBounds(10, 20, 80, 25);
        panel.add(userLabel);

        //输入ip地址的文本域
        final JTextField idText = new JTextField(20);
        idText.setBounds(75, 20, 165, 25);
        panel.add(idText);

        //端口标签
        JLabel portLabel = new JLabel("密码:");
        portLabel.setBounds(10, 50, 80, 25);
        panel.add(portLabel);

        // 输入端口的文本域
        final JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(75, 50, 165, 25);
        panel.add(passwordText);


        JButton loginButton = new JButton("登录");
        loginButton.setBackground(Color.ORANGE);
        loginButton.setBounds(75, 100, 75, 25);
        loginButton.setVerticalAlignment(SwingConstants.CENTER);
        panel.add(loginButton);


        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = idText.getText();
                String password = passwordText.getText();
                String answer;
                try {
                    System.out.println("客户端发送数据");
                    answer = udpSenderThread.send(id, password).trim(); //去除末尾空白，十分重要
                } catch (IOException ex) {
                    answer = "false";
                    ex.printStackTrace();
                }
                System.out.println("登陆检查结果为：" + answer); //+answer.length()+ "  ???" + answer.equals("true")
                if (answer.equals(new String("true"))) {
                    JOptionPane.showMessageDialog(panel, "登陆成功！", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
                    jFrame.dispose();
                } else {
                    JOptionPane.showMessageDialog(panel, "账号或密码错误！", "ERROR", JOptionPane.WARNING_MESSAGE);
                }
            }
        });


        JButton cancelButton = new JButton("退出");
        cancelButton.setBackground(Color.LIGHT_GRAY);
        cancelButton.setBounds(170, 100, 75, 25);
        cancelButton.setVerticalAlignment(SwingConstants.CENTER);
        panel.add(cancelButton);


        //跳转至客户端界面的ActionListener事件
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
                jFrame.dispose();
            }
        });
    }
}
